﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using _004_坦克大战_正式.Properties;

namespace _004_坦克大战_正式
{
    class GameObjectManager
    {

        private static List<NotMovething> wallList = new List<NotMovething>();
        private static List<NotMovething> steelList = new List<NotMovething>();
        private static NotMovething boss;
        private static MyTank myTank;
        private static List<EnemyTank> tankList = new List<EnemyTank>();
        private static List<Bullet> bulletList = new List<Bullet>();
        private static List<Explosion> expList = new List<Explosion>();
        private static int enemyBornSpeed = 120;

        private static int enemyBornCount = 60;

        private static Point[] points = new Point[3];
        public static void Start()
        {
            points = new Point[3];
            points[0].X = 0;
            points[0].Y = 0;

            points[1].X = 6*30;
            points[1].Y = 0;

            points[2].X = 12*30;
            points[2].Y = 0;
        }
        public static void Update()
        {
            foreach (NotMovething nm in wallList)
            {
                nm.Update();
            }
            foreach (NotMovething nm in steelList)
            {
                nm.Update();
            }
            foreach(EnemyTank tank in tankList)
            {
                tank.Update();
            }
            CheckAndDestroyBullet();
            //foreach (Bullet bullet in bulletList)
            //{
            //    bullet.Update();
            //}
            for(int i = bulletList.Count-1; i >= 0; i--)
            {
                Bullet bullet = bulletList[i];
                bullet.Update();
            }
            foreach(Explosion exp in expList)
            {
                exp.Update();
            }

            CheckAndDestroyExplosion();

            boss.Update();

            myTank.Update();

            EnemyBorn();
        }
        private static void CheckAndDestroyBullet()
        {
            List<Bullet> needToDestroy = new List<Bullet>();
            foreach(Bullet bullet in bulletList)
            {
                if (bullet.IsDestroy == true)
                {
                    needToDestroy.Add(bullet);
                }
            }
            foreach(Bullet bullet in needToDestroy)
            {
                bulletList.Remove(bullet);
            }
        }

        private static void CheckAndDestroyExplosion()
        {
            List<Explosion> needToDestroy = new List<Explosion>();
            foreach (Explosion exp in expList)
            {
                if (exp.IsNeedDestroy == true)
                {
                    needToDestroy.Add(exp);
                }
            }
            foreach (Explosion exp in needToDestroy)
            {
                expList.Remove(exp);
            }
        }

        public static void CreateBullet(int x,int y, Tag tag,Direction dir)
        {
            Bullet bullet = new Bullet(x, y, 12, dir, tag);
            bulletList.Add(bullet);
        }
        public static void CreateExplosion(int x,int y)
        {
            Explosion exp = new Explosion(x, y);
            expList.Add(exp);
        }

        public static void DestroyWall(NotMovething wall)
        {
            wallList.Remove(wall);
        } 

        public static void DestroyTank(EnemyTank tank)
        {
            tankList.Remove(tank);
        }

        private static void EnemyBorn()
        {
            enemyBornCount++;
            if (enemyBornCount < enemyBornSpeed) return;

            Random rd = new Random();
            int index = rd.Next(0, 3);
            Point postion = points[index];
            int enemyType = rd.Next(1, 6);
            switch (enemyType)
            {
                case 1:
                    CreateEnemyTank1(postion.X, postion.Y);
                    break;
                case 2:
                    CreateEnemyTank2(postion.X, postion.Y);
                    break;
                case 3:
                    CreateEnemyTank3(postion.X, postion.Y);
                    break;
                case 4:
                    CreateEnemyTank4(postion.X, postion.Y);
                    break;
                case 5:
                    CreateEnemyTank5(postion.X, postion.Y);
                    break;
            }
            enemyBornCount = 0;
        }

        private static void CreateEnemyTank1(int x,int y)
        {
            EnemyTank tank = new EnemyTank(x, y, 2, Resources.GrayDown, Resources.GrayUp, Resources.GrayRight, Resources.GrayLeft);
            tankList.Add(tank);

        }
        private static void CreateEnemyTank2(int x, int y)
        {
            EnemyTank tank = new EnemyTank(x, y, 2, Resources.GreenDown, Resources.GreenUp, Resources.GreenRight, Resources.GreenLeft);
            tankList.Add(tank);
        }
        private static void CreateEnemyTank3(int x, int y)
        {
            EnemyTank tank = new EnemyTank(x, y, 3, Resources.QuickDown, Resources.QuickUp, Resources.QuickRight, Resources.QuickLeft);
            tankList.Add(tank);
        }
        private static void CreateEnemyTank4(int x, int y)
        {
            EnemyTank tank = new EnemyTank(x, y, 1, Resources.SlowDown, Resources.SlowUp, Resources.SlowRight, Resources.SlowLeft);
            tankList.Add(tank);
        }
        private static void CreateEnemyTank5(int x, int y)
        {
            EnemyTank tank = new EnemyTank(x, y, 2, Resources.YellowDown, Resources.YellowUp, Resources.YellowRight, Resources.YellowLeft);
            tankList.Add(tank);
        }



        //public static void DrawMap()
        //{
        //    foreach (NotMovething nm in wallList)
        //    {
        //        nm.DrawSelf();
        //    }
        //    foreach (NotMovething nm in steelList)
        //    {
        //        nm.DrawSelf();
        //    }
        //    boss.DrawSelf();
        //}
        //public static void DrawMyTank()
        //{
        //    myTank.DrawSelf();
        //}

        public static NotMovething IsCollidedWall(Rectangle rt )
        {
            foreach(NotMovething wall in wallList)
            {
                if (wall.GetRectangle().IntersectsWith(rt))
                {
                    return wall;
                }
            }
            return null;
        }
        public static NotMovething IsCollidedSteel(Rectangle rt)
        {
            foreach (NotMovething wall in steelList)
            {
                if (wall.GetRectangle().IntersectsWith(rt))
                {
                    return wall;
                }
            }
            return null;
            
        }

        public static bool IsCollidedBoss(Rectangle rt)
        {
            return boss.GetRectangle().IntersectsWith(rt);
            
        }
        public static MyTank IsCollidedMyTank(Rectangle rt)
        {
            if (myTank.GetRectangle().IntersectsWith(rt)) return myTank;
            else return null;
            
        }
        public static EnemyTank IsCollidedEnemyTank(Rectangle rt)
        {
            foreach(EnemyTank tank in tankList)
            {
                if (tank.GetRectangle().IntersectsWith(rt))
                {
                    return tank;
                }
            }
            return null;
        }
        public static void CreateMyTank()
        {
            int x = 4 * 30;
            int y = 12 * 30;

            myTank = new MyTank(x, y, 5);

        }
        public static void CreateMap()
        {
            CreataWall(1, 1, 4,Resources.wall, wallList);
            CreataWall(3, 1, 4, Resources.wall, wallList);
            CreataWall(5, 1, 3, Resources.wall, wallList);
            CreataWall(6, 2.5, 1, Resources.steel, steelList);
            CreataWall(7, 1, 3, Resources.wall, wallList);
            CreataWall(9, 1, 4, Resources.wall, wallList);
            CreataWall(11, 1, 4, Resources.wall, wallList);
            CreataWall(0, 6.2, 0.5, Resources.steel, steelList);
            CreataWall(2, 6, 1, Resources.wall, wallList);
            CreataWall(3, 6, 1, Resources.wall, wallList);
            CreataWall(5, 5, 1, Resources.wall, wallList);
            CreataWall(7, 5, 1, Resources.wall, wallList);
            CreataWall(9, 6, 1, Resources.wall, wallList);
            CreataWall(10, 6, 1, Resources.wall, wallList);
            CreataWall(12, 6.2, 0.5, Resources.steel, steelList);
            CreataWall(1, 8.1, 3.5, Resources.wall, wallList);
            CreataWall(3, 8.1, 3.5, Resources.wall, wallList);
            CreataWall(5, 7.1, 3, Resources.wall, wallList);
            CreataWall(6, 7.5, 1, Resources.wall, wallList);
            CreataWall(7, 7.1, 3, Resources.wall, wallList);
            CreataWall(9, 8.1, 3.5, Resources.wall, wallList);
            CreataWall(11, 8.1, 3.5, Resources.wall, wallList);
            CreataWall(5.5, 11.5, 1.5, Resources.wall, wallList);
            CreataWall(6.5, 11.5, 1.5, Resources.wall, wallList);
            CreataBoss(6, 12, 1, Resources.Boss);
        }
        private static void CreataBoss(int x, int y, int count, Image img)
        {
            int xPosition = x * 30;
            int yPosition = y * 30;
            boss = new NotMovething((int)xPosition,yPosition, img);
            
        }
        private static void CreataWall(double x, double y, double count,Image img, List<NotMovething> wallList)
        {

            double xPosition = x * 30;
            double yPosition = y * 30;
            for(int i = (int)yPosition; i < yPosition + count * 30; i += 15)
            {
                NotMovething wall1 = new NotMovething((int)xPosition, i, img);
                NotMovething wall2 = new NotMovething((int)xPosition+15, i, img);
                wallList.Add(wall1);
                wallList.Add(wall2);
            }
        }


        public static void KeyDown(KeyEventArgs args)
        {
            myTank.KeyDown(args);
        }
        public static void KeyUp(KeyEventArgs args)
        {
            myTank.KeyUp(args);
        }
    }
}
